package org.example.dao.imp;

import org.example.dao.inter.StudentDao;
import org.example.entitys.managers.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/13
 */

@Repository
public class StudentDaoImp implements StudentDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int insert(Student student) {
        String sql = "insert into t_manager_student(`stu_name`,`from_place`,`parent_phone`," +
                "`current_grade`,`status`,`login_id`,`comment`) values (?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql,
                student.getStu_name(),
                student.getFrom_place(),
                student.getParent_phone(),
                student.getCurrent_grade(),
                student.getStatus(),
                student.getLogin_id(),
                student.getComment()
        );
    }

    @Override
    public int update(Student student) {
        String sql = "update t_manager_student set stu_name = ?, from_place = ?, parent_phone = ?," +
                "current_grade = ?, status = ?, login_id = ?, comment = ? where id = ?";
        return jdbcTemplate.update(sql,
                student.getStu_name(),
                student.getFrom_place(),
                student.getParent_phone(),
                student.getCurrent_grade(),
                student.getStatus(),
                student.getLogin_id(),
                student.getComment(),
                student.getId()
        );
    }

    @Override
    public int delete(int id) {
        String sql = "delete from t_manager_student where id = ? ";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public boolean isExitById(int id) {
        String sql = "select count(*) from t_manager_student where id=?";
        return jdbcTemplate.queryForObject(sql, Integer.class, id) > 0;
    }

    @Override
    public Student queryById(int id) {
        String sql = "select id, stu_name, from_place, parent_phone, current_grade," +
                " status, login_id, comment from t_manager_student where id=?";
        return jdbcTemplate.queryForObject(sql, this::transform, id);
    }

    @Override
    public int queryLastInsertID() {
        String sql = "select LAST_INSERT_ID()";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    @Override
    public List<Student> queryAll() {
        String sql = "select id, stu_name, from_place, parent_phone, current_grade, " +
                "status, login_id, comment from t_manager_student";
        return jdbcTemplate.query(sql, this::transform);
    }

    @Override
    public int checkStudent(int id) {
        String sql = "update t_manager_student set status = 0 where id = ? ";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public List<Student> queryAllReviewedStudents() {
        String sql = "select id, stu_name, from_place, parent_phone, current_grade, status, login_id, comment" +
                " from t_manager_student where status = 1 ";
        return jdbcTemplate.query(sql, this::transform);
    }

    @Override
    public Student transform(ResultSet rs, int RowNum) throws SQLException {
        return new Student(
                rs.getInt("id"),
                rs.getString("stu_name"),
                rs.getString("from_place"),
                rs.getString("parent_phone"),
                rs.getString("current_grade"),
                rs.getInt("status"),
                rs.getInt("login_id"),
                rs.getString("comment")
        );
    }
}
